<?php

class DepartmentHelper extends Helper {
    
    private $check_rule;
    private $_dpfield;
    
    public function __construct() {
        $this->check_rule = array(
            'dp_code' => array('require', '部门编号不能为空'),
            'dp_name' => array('require', '部门名称不能为空'),
            'dp_province' => array('require', '省份不能为空'),
            'dp_city' => array('require', '城市名称不能为空')
        );
        $this->_dpfield = array('dp_id', 'dp_code', 'dp_name', 'dp_province', 'dp_city', 'dp_bank', 'dp_status');
    }
    /**
     * 单条数据
     * @param type $where
     * @return type
     */
    public function dpRow($where){
        if(is_numeric($where)){
            $where = array('`dp_id` = ?' => $where);
        }
        $departmentModel = $this->LoadModel('Public'); 
        $data = $departmentModel->getOneRow('department', $this->_dpfield, $where);
        if(!empty($data['dp_bank'])){
            $data['dp_bank_arr'] = explode(',', $data['dp_bank']);
        }
        return $data;
    }
    /**
     * 所有
     * @param type $where
     * @return type
     */
    public function dpAll($where){
        $departmentModel = $this->LoadModel('Public'); 
        $data = $departmentModel->getList('department', $this->_dpfield, $where);
        if(!empty($data['dp_bank'])){
            $data['dp_bank_arr'] = explode(',', $data['dp_bank']);
        }
        return $data;
    }
    
    /**
     * 部门数据保存
     * @param type $data
     * @param type $where
     * @return type
     */
    public function dpSave($data, $where = ''){
        if(is_numeric($where)){
            $where = array('`dp_id` = ?' => $where);
        }
        $validator = Validator::checkRule($data, $this->check_rule);
        if(!$validator['pass']){
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $departmentModel = $this->LoadModel('Public'); 
        if(is_array($data['dp_bank'])) $data['dp_bank'] = implode (',', $data['dp_bank']);
        if(empty($where)){
            $id = $departmentModel->save('department', $data);
            if($id){
                return array('success' => true, 'msg' => $id);
            }else{
                return array('success' => false, 'msg' => '保存失败');
            }
        }else{
            $departmentModel->update('department', $data, $where);
            return array('success' => true);
        }
    }
    /**
     * 分页
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function pageList($where, $page = 1, $limit = PAGE_SIZE, $param = array()){
        $departmentModel = $this->LoadModel('Public'); 
        $data['count'] = $departmentModel->getRowCount('department', $where);
        if(!empty($data['count'])){
            $data['all'] = $departmentModel->getList('department', $this->_dpfield, $where, array($page, $limit), NULL, array('dp_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }
    /**
     * 删除
     * @param type $where
     * @return type
     */
    public function dpRemove($where){
        if(is_numeric($where)){
            $where = array('`dp_id` = ?' => $where);
        }
        $departmentModel = $this->LoadModel('Public'); 
        $do = $departmentModel->update('department', array('dp_status' => 'del'), $where);
        return $do;
    }
}